package main

import (
	"io/ioutil"
	"log"

	"github.com/buger/jsonparser"
)

func readFileFrompath(path string) []byte {
	content, err := ioutil.ReadFile(path)
	if err != nil {
		log.Println(err)
	}
	return content
}

func main() {
	log.Println("-------------------------------------------------------------------")
	log.Println("去除转义测试 func Unescape(in, out []byte) ([]byte, error)")
	log.Println("-------------------------------------------------------------------")
	unescape_src := readFileFrompath("./resources/unescape.json")
	unescape_dst, err := jsonparser.Unescape(unescape_src, nil)
	if err != nil {
		log.Fatalln(err)
	}
	log.Println("payload_src:", string(unescape_src))
	log.Println("payload_dst:", string(unescape_dst))
	log.Println("-------------------------------------------------------------------")
	log.Println("测试数据")
	log.Println("-------------------------------------------------------------------")
	payload := readFileFrompath("./resources/test.json")
	log.Println("Src Data:", string(payload))

	log.Println("-------------------------------------------------------------------")
	log.Println("获取测试测试 func Get(data []byte, keys ...string) (value []byte, dataType ValueType, offset int, err error)")
	log.Println("-------------------------------------------------------------------")
	var valueBuffer []byte
	var valueType jsonparser.ValueType
	var ValueOffset int
	var ValueError error
	valueBuffer, valueType, ValueOffset, ValueError = jsonparser.Get(payload, "string")
	log.Println(string(valueBuffer), valueType, ValueOffset, ValueError)
	valueBuffer, valueType, ValueOffset, ValueError = jsonparser.Get(payload, "number")
	log.Println(string(valueBuffer), valueType, ValueOffset, ValueError)
	valueBuffer, valueType, ValueOffset, ValueError = jsonparser.Get(payload, "bool_true")
	log.Println(string(valueBuffer), valueType, ValueOffset, ValueError)
	valueBuffer, valueType, ValueOffset, ValueError = jsonparser.Get(payload, "bool_false")
	log.Println(string(valueBuffer), valueType, ValueOffset, ValueError)
	valueBuffer, valueType, ValueOffset, ValueError = jsonparser.Get(payload, "null")
	log.Println(string(valueBuffer), valueType, ValueOffset, ValueError)
	valueBuffer, valueType, ValueOffset, ValueError = jsonparser.Get(payload, "object")
	log.Println(string(valueBuffer), valueType, ValueOffset, ValueError)
	valueBuffer, valueType, ValueOffset, ValueError = jsonparser.Get(payload, "array")
	log.Println(string(valueBuffer), valueType, ValueOffset, ValueError)
	valueBuffer, valueType, ValueOffset, ValueError = jsonparser.Get(payload, "error")
	log.Println(string(valueBuffer), valueType, ValueOffset, ValueError)
	valueBuffer, valueType, ValueOffset, ValueError = jsonparser.Get(payload, "object", "string")
	log.Println(string(valueBuffer), valueType, ValueOffset, ValueError)
	valueBuffer, valueType, ValueOffset, ValueError = jsonparser.Get(payload, "array", "[4]")
	log.Println(string(valueBuffer), valueType, ValueOffset, ValueError)
	valueBuffer, valueType, ValueOffset, ValueError = jsonparser.Get(payload, "array", "4")
	log.Println(string(valueBuffer), valueType, ValueOffset, ValueError)
	log.Println("-------------------------------------------------------------------")
	log.Println("测试 func GetString(data []byte, keys ...string) (val string, err error)")
	log.Println("-------------------------------------------------------------------")
	{
		val, err := jsonparser.GetString(payload, "string")
		log.Println(val, err)
	}
	{
		val, err := jsonparser.GetString(payload, "number")
		log.Println(val, err)
	}
	{
		val, err := jsonparser.GetString(payload, "object")
		log.Println(val, err)
	}
	{
		val, err := jsonparser.GetString(payload, "null")
		log.Println(val, err)
	}
	{
		val, err := jsonparser.GetString(payload, "error")
		log.Println(val, err)
	}
	{
		val, err := jsonparser.GetString(payload, "object", "string")
		log.Println(val, err)
	}
	log.Println("-------------------------------------------------------------------")
	log.Println("测试 func GetString(data []byte, keys ...string) (val string, err error)")
	log.Println("-------------------------------------------------------------------")
	payload, err = jsonparser.Set(payload, []byte("\"string\""), "new")
	if err != nil {
		log.Fatalln(err)
	}
	log.Println("payload:", string(payload))
	payload, err = jsonparser.Set(payload, []byte("\"string\""), "object", "new")
	if err != nil {
		log.Fatalln(err)
	}
	log.Println("payload:", string(payload))
	log.Println("-------------------------------------------------------------------")
	log.Println("-------------------------------------------------------------------")
	log.Println("测试 func Delete(data []byte, keys ...string) []byte")
	log.Println("-------------------------------------------------------------------")
	payload = jsonparser.Delete(payload, "new")
	log.Println("payload:", string(payload))
	payload = jsonparser.Delete(payload, "object", "new")
	log.Println("payload:", string(payload))
	log.Println("-------------------------------------------------------------------")
	// jsonparser.StringToBytes()
	// jsonparser.WriteToBuffer()
	jsonparser.ArrayEach()
	jsonparser.EachKey()
	jsonparser.ObjectEach()
}
